<template>
  <!-- ref = wrapper ? -->
  <div class="list" ref="wrapper">
    <!-- 把需要滚动的内容，包裹在一个 div 中 -->
    <div>
      <!-- 当前城市区域 -->
      <div class="area">
        <div class="title">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <!-- this.$store.state.city -->
            <div class="button">{{this.currentCity}}</div>
          </div>
        </div>
      </div>
      <!-- 热门城市区域 -->
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div
            class="button-wrapper"
            v-for="item of hotCities"
            :key="item.id"
            @click="handleCityClick(item.name)"
          >
            <div class="button">{{item.name}}</div>
          </div>
        </div>
      </div>
      <!-- 所有城市 -->
      <!-- 注意数组和对象被 for...of 循环遍历时的区别 -->
      <!-- cities 里面的数据都是键值对的形式 -->
      <div class="area" v-for="(item, key) of cities" :key="key" :ref="key">
        <div class="title">{{key}}</div>
        <div class="item-list">
          <div
            class="item"
            v-for="innerItem of item"
            :key="innerItem.id"
            @click="handleCityClick(innerItem.name)"
          >{{innerItem.name}}</div>
        </div>
        <!-- <div class="item-list">
          <div class="item" v-for="innerItem of item" :key="innerItem.id">{{innerItem.name}}</div>
        </div> -->
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'

export default {
  name: 'cityList',
  props: {
    cities: Object,
    hotCities: Array,
    letter: String
  },
  computed: {
    ...mapState({
      // 把 vux 里面 city 这个公用数据映射到 List 组件的计算属性里
      // 映射过来的名字叫做 currentCity
      currentCity: 'city'
    })
  },
  methods: {
    handleCityClick (city) {
      // 这里没有异步操作，不需要调用 actions
      // this.$store.dispatch('changeCity', city)
      // this.$store.commit('changeCity', city)
      // vuex 为我们提供的简便写法，通过 mapMutations
      this.changeCity(city)
      // console.log('被点击了！')
      this.$router.push('/')
    },
    // 把 mutation 映射到 List 组件中一个叫 changeCity 的方法里
    ...mapMutations(['changeCity'])
  },
  // 挂载完毕后执行
  mounted () {
    // this.$refs.wrapper ?
    // this.scroll = new Bscroll(this.$refs.wrapper)
    setTimeout(() => {
      this.scroll = new Bscroll(this.$refs.wrapper, {
        click: true
      })
    }, 100)
  },
  // 借助侦听器
  watch: {
    letter () {
      // console.log(this.letter)
      // Bscroll 提供的方法，可以自动滚到对应的字母栏
      if (this.letter) {
        // this.$refs[this.letter] 打印的结果为数组
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    }
  }
}
</script>

<style scoped>
  .list {
    /* 不是很理解这里 */
    /* 不想盒子被撑开 */
    overflow: hidden;
    position: absolute;
    top: 80px;
    left: 0;
    right: 0;
    bottom: 0;
  }
  .area .title {
    height: 32px;
    line-height: 32px;
    background: #eee;
    padding-left: 4px;
    color: #666;
    font-size: 16px;
  }
  .button-list {
    overflow: hidden;
    padding: 2px 8px 2px 2px;
  }
  .button-wrapper {
    float: left;
    width: 31%;
  }
  .button {
    /* float: left; */
    margin: 5px;
    padding: 8px 0;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 5px;
    font-size: 16px;
  }
  .item-list .item {
    padding-left: 8px;
    border-bottom: 1px solid #ccc;
    line-height: 38px;
    font-size: 16px;
  }
</style>
